//
//  TableViewC_CaiNiXiHuan.m
//  HappyStore
//
//  Created by 点点充 on 16/1/14.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "TableViewC_CaiNiXiHuan.h"
#import "SearchProjectInfoVC.h"
#import "projectDetailVC.h"

#import "LSBecomeVIPVC.h"
#import "LSRechargeVC.h"
#import "ForgetPassWord.h"
#import "LSPayResultVC.h"

//首页支付全局字典
NSDictionary *allConstDict;

@interface TableViewC_CaiNiXiHuan (){

    NSString * or_Number;//订单号
    BOOL  isSetPayPass;//是否有支付密码
    NSString * projectId;//项目标识
    double price;//价格

}
@property (strong, nonatomic) LSPromptAlertVC * LSPromptAlert;
@end


@implementation TableViewC_CaiNiXiHuan

-(LSPromptAlertVC *)LSPromptAlert{
    
    if (!_LSPromptAlert) {
        _LSPromptAlert = [[LSPromptAlertVC alloc]init];
    }
    return _LSPromptAlert;
    
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];
    
    //支付回调通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentViewWithAlipay) name:@"alipayResult" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentFailedView) name:@"alipayFailed" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _aryInfo = [NSArray array];
    
}

#pragma mark - 支付回调
-(void)presentFailedView{
    
    // 跳转至支付失败页面
    LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
    [LSPayResult setCashTypeWithCashFailed];
    [_homeVC.navigationController pushViewController:LSPayResult animated:YES];
    
}

-(void)presentViewWithAlipay{
    
    // 跳转至支付成功页面
    LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
    [LSPayResult setCashTypeWithCashSuccess];
    [_homeVC.navigationController pushViewController:LSPayResult animated:YES];
    
}
- (void)setDelegte
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _aryInfo.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 102;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellLoveSingle = @"cellLoveSingle";//首页
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellLoveSingle forIndexPath:indexPath];;
    

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    UILabel *titleLabel = [cell viewWithTag:21];
    titleLabel.text = _aryInfo[indexPath.row][@"title"];
    
    UILabel *addressLabel = [cell viewWithTag:23];
    addressLabel.text = _aryInfo[indexPath.row][@"address"];
    UILabel *moneyLabel = [cell viewWithTag:24];
    
    if ([[CustomExcetion comperNumber:_aryInfo[indexPath.row][@"money"]] isEqualToString:@"0元"]) {
        moneyLabel.text = @"面议";
    }else{
        moneyLabel.text = [CustomExcetion comperNumber:_aryInfo[indexPath.row][@"money"]];
    }
    
    
    UILabel *industryLabel = [cell viewWithTag:22];
    industryLabel.text = _aryInfo[indexPath.row][@"Industry"];
    
    if ([_aryInfo[indexPath.row][@"type"] isEqualToString:@"普通项目"]) {
        
        if ([_aryInfo[indexPath.row][@"userType"] isEqualToString:@"1"]) {
            
            UIImageView *imgView = [cell viewWithTag:20];
            imgView.image = [UIImage imageNamed:@"个人"];
            
        }else if ([_aryInfo[indexPath.row][@"userType"] isEqualToString:@"2"]){
            
            UIImageView *imgView = [cell viewWithTag:20];
            imgView.image = [UIImage imageNamed:@"企业"];
            
        }else if ([_aryInfo[indexPath.row][@"userType"] isEqualToString:@"3"]){
            
            UIImageView *imgView = [cell viewWithTag:20];
            imgView.image = [UIImage imageNamed:@"政府"];
            
        }
    
    }else{
        
        UIImageView *imgView = [cell viewWithTag:20];
        imgView.image = [UIImage imageNamed:@"组-49"]; 
 }
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    
    if ([_aryInfo[indexPath.row][@"type"] isEqualToString:@"普通项目"]) {
        
        if (allConstMemberid) {
        
            [self showRoundProgressWithTitle:@"请稍后..."];
            
            NSMutableDictionary * dict = [NSMutableDictionary dictionary];
            dict[@"projectId"] = _aryInfo[indexPath.row][@"ID"];
            dict[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
            
            [LSHttpTool sendHttpData:@"projectDetail_app_project.im" params:dict success:^(id responseObject) {
                NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
                
                if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                    
                    [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
                    
                    SearchProjectInfoVC * SeaVC = [[SearchProjectInfoVC alloc]initWithNibName:@"SearchProjectInfoVC" bundle:nil];
                    SeaVC.dataDict = dicInfo[@"data"];
                    SeaVC.projectData = [NSMutableArray arrayWithArray:dicInfo[@"data"][@"commentsList"]];
                    
                    [_homeVC.navigationController pushViewController:SeaVC animated:YES];
                    
                }else if ([[dicInfo objectForKey:@"result"] integerValue] == 2){

                    [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
                    
                }else if ([[dicInfo objectForKey:@"result"]integerValue] == 3){
                    
                    isSetPayPass = [dicInfo[@"data"][@"isSetPayPass"] boolValue];
                    price = [dicInfo[@"data"][@"powerMoney"]floatValue];
                    
                    projectId = _aryInfo[indexPath.row][@"ID"];
                    self.LSPromptAlert.modalPresentationStyle=UIModalPresentationOverCurrentContext;
                    self.LSPromptAlert.PromptAlertView.alipayBtn.hidden = YES;
                    [_homeVC presentViewController:self.LSPromptAlert animated:YES completion:nil];
                    
                    //监听按钮
                    [self.LSPromptAlert.PromptAlertView.becomeVIPBtn addTarget:self action:@selector(clickBecomeVIPBtn) forControlEvents:UIControlEventTouchUpInside];
                    
                    [self.LSPromptAlert.PromptAlertView.alipayBtn addTarget:self action:@selector(clickAlipayBtn) forControlEvents:UIControlEventTouchUpInside];
                    
                    [self.LSPromptAlert.PromptAlertView.balancePayBtn addTarget:self action:@selector(clickCashBtn) forControlEvents:UIControlEventTouchUpInside];
                    
                }
                
                [self hideBubble];
            } failure:^(NSError *error) {

                [self showSingleIconBubble:@"网络连接错误"];
                
            }];
        
        }else{
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录", nil];
        
        alert.tag = 10;
        
        [alert show];
        
        }
        
    }else if ([_aryInfo[indexPath.row][@"type"] isEqualToString:@"ppp项目"]){
        
        projectDetailVC *projectVC = [[projectDetailVC alloc]init];
        projectVC.typeID = _aryInfo[indexPath.row][@"ID"];
//        projectVC.userId = allConstMemberid;

        [_homeVC .navigationController pushViewController:projectVC animated:YES];
    
    }
    

    
}

#pragma mark - 弹框按钮监听
//跳转会员界面
-(void)clickBecomeVIPBtn{
    
    [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
    
    LSBecomeVIPVC * LSBecomeVIP = [[LSBecomeVIPVC alloc]init];
    [_homeVC.navigationController pushViewController:LSBecomeVIP animated:YES];
    
}

#pragma mark - 支付回调通知
-(void)clickAlipayBtn{
    NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
    dic[@"token"] = allConstMemberToken;
    dic[@"payUserId"] = allConstMemberid;
    dic[@"payWay"] = @"2";
    dic[@"payMoney"] = @(price);
    dic[@"resId"] = projectId;
    dic[@"order_no"] = or_Number;
    allConstDict = dic;
    or_Number = [LSAlipay alipay:@"乐商网" body:@"项目详情" price:0.01f];
    
    [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 余额支付
-(void)clickCashBtn{
    
    NSString * balanceStr = [self.LSPromptAlert.PromptAlertView.balanceLabel.text stringByReplacingOccurrencesOfString:@"￥"withString:@""];
    
    double balance = [balanceStr floatValue];
    
    if (balance < 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付失败"
                                                        message:@"余额不足！"
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"立刻充值",nil];
        alert.tag = 20;
        [alert show];
        
    }else{
        
        if (isSetPayPass) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付失败"
                                                            message:@"未设置支付密码"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"立即设置",nil];
            alert.tag = 30;
            [alert show];
            
        }else{
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"余额支付" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            
            UITextField *title = [alert textFieldAtIndex:0];
            [title setSecureTextEntry:YES];
            title.borderStyle = UITextBorderStyleNone;
            title.textColor = [UIColor redColor];
            title.textAlignment = NSTextAlignmentCenter;
            title.font = [UIFont systemFontOfSize:16.0f];
            title.placeholder = @"请输入支付密码";
            
            
            [alert showAlertWithCompletionHandler:^(NSInteger buttonIndex) {
                if(buttonIndex == 1){
                    UITextField *tf=[alert textFieldAtIndex:0];
                    if (tf.text.length > 0) {
                        
                        [self showRoundProgressWithTitle:@"请稍后..."];
                        
                        NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
                        dic[@"token"] = allConstMemberToken;
                        dic[@"payUserId"] = allConstMemberid;
                        dic[@"payWay"] = @"1";
                        dic[@"payMoney"] = @(price);
                        dic[@"resId"] = projectId;
                        dic[@"payPass"] = tf.text;
                        
                        
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"tableView_loveNotBlance" object:nil userInfo:dic];
                    [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
                        
                    }else{
                        
                        NSString *strMsg = @"密码不能为空";
                        
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:strMsg delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
                        [alert show];
                        
                    }
                }
            }];
            
            
        }
        
    }
    
}

#pragma mark - alertview

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
        
        UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *loginNAV = [LoginAndRegister instantiateViewControllerWithIdentifier:@"LoginNav"];
        //充值
        LSRechargeVC * LSRecharge = [[LSRechargeVC alloc]init];
        
        switch (alertView.tag) {
            case 10:
                
                [self presentViewController:loginNAV animated:YES completion:nil];
                
                break;
                
            case 20:
                
                [_homeVC.navigationController pushViewController:LSRecharge animated:YES];
                
                break;
                
            case 30:
            {
                
                UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                ForgetPassWord *ForgetPassWord = [LoginAndRegister instantiateViewControllerWithIdentifier:@"forgetpassword"];
                ForgetPassWord.titleStr = @"修改支付密码";
                [_homeVC.navigationController pushViewController:ForgetPassWord animated:YES];
                
            }
                break;
                
            default:
                break;
        }
    }
    
    
}




@end
